$(document).ready(function() {
      $("#search_textbox").autocomplete({
   source: "./API/search_autocomplete.php",
   minLength: 2//search after two characters
   /*select: function(event,ui){
    $("search_textbox").value = $value;
    }*/
  });
  $(document).ready(function(){
    $( "#checkin" ).datepicker("setDate","0d");
    $( "#checkout" ).datepicker("setDate","+1d");
  });
    $( "#checkin" ).datepicker("setDate","0d");
    $( "#checkout" ).datepicker("setDate","+1d");
      var todaysDate = new Date();

        $( "#checkin" ).datepicker({
            minDate: "0d",
            maxDate: "+499d",
            onSelect:
                function(selectedDate){
                    if($('#checkin').datepicker('getDate') != null){
                        var currentDate = $('#checkin').datepicker('getDate');
                        currentDate.setDate(currentDate.getDate()+1);
                        $('#checkout').datepicker('option', 'minDate', currentDate);
                        var maxDate= $('#checkin').datepicker('getDate');
                        maxDate.setDate(maxDate.getDate()+27);
                        $('#checkout').datepicker('option', 'maxDate', maxDate);
                    }
                },
            onClose:
                function(selectedDate){
                    var currentDate = $('#checkin').datepicker('getDate');
                    var item_length = jQuery('#checkin').val().length;
                    if(currentDate == null || item_length != 10){
                        $('#checkin').datepicker('setDate', "0d");
                    }
                }
        });


        $( "#checkout" ).datepicker({
            minDate: "+1d",
            maxDate: "+5",
            beforeShow:
                function(selectedDate){
                    if($('#checkin').datepicker('getDate') != null){
                        var currentDate = $('#checkin').datepicker('getDate');
                        currentDate.setDate(currentDate.getDate()+1);
                        $('#checkout').datepicker('option', 'minDate', currentDate);
                        var maxDate= $('#checkin').datepicker('getDate');
                        maxDate.setDate(maxDate.getDate()+27);
                        $('#checkout').datepicker('option', 'maxDate', maxDate);
                    }
                },
            onClose:
                function(selectedDate){ 
                    var check_in_date = $('#checkin').datepicker('getDate');
                    var check_out_date = $('#checkout').datepicker('getDate');
                    var maxDate = $( "#checkout" ).datepicker( "option", "maxDate" );
                    var item_length = jQuery('#checkout').val().length;
                    if ((check_in_date > check_out_date) || ( check_out_date > maxDate) || (item_length != 10 )) {
                        $('#checkout').datepicker('setDate', check_in_date);
                    }
                }
        });
});


function search_validate() {
    if(document.getElementById('search_textbox').value.length <= 0){
        document.getElementById('search_textbox').style.border="2px solid #F00";
        document.getElementById('search_error').innerHTML="Please enter a location."
    } else {
        document.getElementById("search_form").submit();
    }
};

function checkDestination() {
    if(document.getElementById('search_textbox').value.length <= 0){
        document.getElementById('search_textbox').style.border="2px inset #F00";
        document.getElementById('search_error').innerHTML="Please enter a location.";
    } else {
        document.getElementById('search_textbox').style.border="2px inset #000";
        document.getElementById('search_error').innerHTML="";
    }
};
